﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;

namespace BLL
{
    public class StudentBLL
    {
        public List<Student> GetAll()
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.Students.OrderByDescending(p => p.FirstName).ToList();
            }
        }

        public List<Student> GetAll(bool isEnable)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.Students.Where(p => p.isEnable == isEnable).OrderByDescending(p => p.FirstName).ToList();
            }
        }

        //Get by category

        public List<Student> GetByCategory(int idCategory)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.Students.Where(p => p.ClassId == idCategory).OrderByDescending(p => p.FirstName).ToList();
            }
        }

        public List<Student> GetByCategory(int idCategory, bool IsEnable)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.Students.Where(p => p.ClassId == idCategory && p.isEnable == IsEnable).OrderByDescending(p => p.FirstName).ToList();
            }
        }
        public List<Student> GetByCategorys(int idCategory, int idNow)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.Students.Where(p => p.ClassId == idCategory && p.Id != idNow).OrderByDescending(p => p.FirstName).ToList();
            }
        }

        public List<Student> GetByCategory(int cateId, bool isEnable, int page, int size, out int countItem)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                int skip = (page - 1) * size;
                List<Student> list = data.Students.Where(p => p.ClassId == cateId && p.isEnable == isEnable).OrderByDescending(p => p.FirstName).ToList();
                countItem = list.Count;
                return list.Skip(skip).Take(size).ToList();
            }
        }

        public List<Student> GetByCateParentId(int cateId, bool isEnable)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                List<Class> list = data.Classes.Where(p => p.CateClassId == cateId && p.isEnable == true).ToList();
                List<Student> listStudent = new List<Student>();
                foreach (var item in list)
                {
                    List<Student> student = GetByCategory(item.Id);
                    listStudent.AddRange(student);
                }
                return listStudent.OrderBy(p => p.FirstName).ToList();
            }
        }

        public List<Student> GetByCateParentId(int cateId)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                List<Class> list = data.Classes.Where(p => p.CateClassId == cateId).ToList();
                List<Student> listStudent = new List<Student>();
                foreach (var item in list)
                {
                    List<Student> student = GetByCategory(item.Id);
                    listStudent.AddRange(student);
                }
                return listStudent.OrderBy(p => p.FirstName).ToList();
            }
        }
        //Get by page

        public List<Student> GetByPage(List<Student> list, int page, int size)
        {
            page--;
            int skip = page * size;
            return list.Skip(skip).Take(size).ToList();
        }

        //Get by Tag

        //Get by id

        public Student GetById(int id)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.Students.Where(p => p.Id == id).FirstOrDefault();
            }
        }
        public Student GetById(int id, bool isEnable)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.Students.Where(p => p.Id == id && p.isEnable == isEnable).FirstOrDefault();
            }
        }

        //Enable

        public void Enable(int id)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                Student obj = data.Students.Where(p => p.Id == id).FirstOrDefault();
                obj.isEnable = !obj.isEnable;

                data.SubmitChanges();
            }
        }

        public void Enable(int id, bool isEnable)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                Student obj = data.Students.Where(p => p.Id == id).FirstOrDefault();
                obj.isEnable = isEnable;

                data.SubmitChanges();
            }
        }

        //Add View

        //Add

        public void Add(Student obj)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                data.Students.InsertOnSubmit(obj);

                data.SubmitChanges();
            }
        }

        //Update

        public void Update(Student obj)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                Student students = data.Students.Where(p => p.Id == obj.Id).FirstOrDefault();

                students.Address = obj.Address;
                students.Brithday = obj.Brithday;
                students.ClassId = obj.ClassId;
                students.CMND = obj.CMND;
                students.CreateDate = obj.CreateDate;
                students.FirstName = obj.FirstName;
                students.Images = obj.Images;
                students.isEnable = obj.isEnable;
                students.LastName = obj.LastName;
                students.Phone = obj.Phone;
                students.Sex = obj.Sex;

                data.SubmitChanges();
            }
        }

        //Delete

        public void Delete(int id)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                Student obj = data.Students.Where(p => p.Id == id).FirstOrDefault();
                if (obj != null)
                {
                    data.Students.DeleteOnSubmit(obj);
                    data.SubmitChanges();
                }
            }
        }

    }
}
